﻿using AiXiu.Common;
using AiXiu.IDAL;
using AiXiu.Model;
using Aliyun.Acs.Core;
using Aliyun.Acs.vod.Model.V20170321;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AiXiu.DAL
{
    public class VodService : IVodService
    {
        /// <summary>
        /// 删除数据库
        /// </summary>
        /// <param name="videoId"></param>
        public void DeleteVideo(string videoId)
        {
            AiXiuDbConten dbConten = new AiXiuDbConten();
            //查询数据库对应Id
            TBvideos videoEntity = dbConten.TBvideos.SingleOrDefault(p => p.VideoId == videoId);
            if (videoEntity != null)
            {
                //删除方法
                dbConten.Entry(videoEntity).State = System.Data.Entity.EntityState.Deleted;
                //进行删除
                dbConten.SaveChanges();
            }
        }

        public List<string> GetInProcessVideoIds()
        {
            List<string> queryStatus = new List<string>()
            {
                ((int)VideoStatus.Uploading).ToString(),
                ((int)VideoStatus.UploadSucces).ToString(),
                ((int)VideoStatus.Transcoding).ToString()
            };
            AiXiuDbConten dbConten = new AiXiuDbConten();
            //获取视频ID，状态为上传中、上传完成、转码中、
            List<TBvideos> videos = dbConten.TBvideos.Where(p => queryStatus.Contains(p.Status)).ToList();
            List<string> strings = videos.Select(a => a.VideoId).ToList();
            return strings;
        }

        /// <summary>
        /// 通过视频ID获取视频信息
        /// </summary>
        /// <param name="videoId">视频ID</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public TBvideos GetVideoByVideoId(string videoId)
        {
            AiXiuDbConten dbConten = new AiXiuDbConten();
            TBvideos video = dbConten.TBvideos
                .Include(p => p.TBUsers)
                .SingleOrDefault(p => p.VideoId == videoId);
            return video;
        }

        public List<TBvideos> GetVideosByUserId(int userId)
        {
            AiXiuDbConten dbContext = new AiXiuDbConten();
            List<TBvideos> list = dbContext.TBvideos.Where(a => a.UserId == userId).OrderByDescending(a => a.UploadTime).ToList();
            return list;
        }

        public List<TBvideos> GetVidos()
        {
            AiXiuDbConten dbConten = new AiXiuDbConten();
            int statusValue = (int)VideoStatus.Normal;
            return dbConten.TBvideos//查询视频信息
                .Where(p => p.Status == statusValue + "")
                .Include(p => p.TBUsers)//连接数据库
                .OrderByDescending(p => p.UploadTime)
                .ToList();
        }

        public TBvideos ToBeUpload(string videoId, int userId, string headline, string loaction = null)
        {
            if (string.IsNullOrWhiteSpace(videoId))
            {
                throw new ArgumentException(nameof(videoId));
            }
            if (string.IsNullOrWhiteSpace(headline))
            {
                throw new ArgumentException(nameof(headline));
            }
            TBvideos videoEntity = new TBvideos()
            {
                VideoId = videoId,
                UserId = userId,
                Headline = headline,
                Location = loaction,
                Status = ((int)VideoStatus.Uploading) + "",
                UploadTime = DateTime.Now
            };
            AiXiuDbConten dbConten = new AiXiuDbConten();
            dbConten.Entry(videoEntity).State = System.Data.Entity.EntityState.Added;
            dbConten.SaveChanges();
            return videoEntity;
        }

        public async void UpdateVideos(List<TBvideos> videos)
        {
            AiXiuDbConten dbConten = new AiXiuDbConten();
            TBvideos videoEntity;
            foreach (TBvideos video in videos)
            {
                videoEntity = dbConten.TBvideos.SingleOrDefault(p => p.VideoId == video.VideoId);
                if (videoEntity != null)
                {
                    videoEntity.CoverURL = video.CoverURL?.Substring(0, video.CoverURL.IndexOf("?"));
                    videoEntity.Status = video.Status;
                }
                dbConten.Entry(videoEntity).State = EntityState.Modified;
                await dbConten.SaveChangesAsync();
            }
        }
    }
}
